/**
 * 数组对象去重
 * @param {*} dataArray 
 * @param {*} id 
 * @returns 
 */
export function reduce(dataArray,id = 'id') {
    let result = [];
    for (let i = 0; i < dataArray.length; i++) {
        if (result.indexOf(dataArray[i][id]) == -1) {
            result.push(dataArray[i][id]);
        } else {
            dataArray.splice(i, 1);
        };
    };
    return dataArray;
};

/**
 * 数组对象去重保留后者
 * @param {*} arr 
 * @param {*} name 
 * @returns 
 */

export function arrayUnique(arr, name) {
	let hash = {}
	return arr.reduce( (acc, cru, index) => {
		if ( !hash[cru[name]] ) {
			hash[cru[name]] = {index:acc.length} 
			acc.push(cru)
		}else{
			acc.splice( hash[cru[name]]['index'], 1, cru)
		}
		return acc;
	}, [])
}

//树结构 查询节点所在level 为该节点加levelIndex属性
export const convertMaplevelIndex=(data) =>{
  const dataToTree1 = (pp) => {
    if (!pp.children) {
      return;
    }

    if (pp.children && pp.children.length > 0) {
      pp.children.map((item, index) => {
        pp.children[index].levelIndex = pp.levelIndex + "|" + index;
        dataToTree1(pp.children[index]);
      });
    }
    return;
  };
  data.map((item, index) => {
    data[index].levelIndex = index + "";
    dataToTree1(data[index]);
  });
}
// 
/**
 * 树形结构
 * @param {*} data 
 * @param {*} id 
 * @param {*} pid 
 * @returns 
 */
export const metaTree = (data,id = 'id',pid = 'pid',label=false) => {
    let parents = data.filter((p) => p[pid] === 0),
      children = data.filter((c) => c[pid] !== 0);
    const dataToTree = (parents, children) => {
      parents.map((p) => {
        if(label){
          p.value = p[id]
          p.label = p[label]
        }
        p.id = p[id]
        p.pid = p[pid]
        children.map((c, i) => {
          if (c[pid] === p[id]) {
            let _c = JSON.parse(JSON.stringify(children));
            _c.splice(i, 1);
            dataToTree([c], _c);
            if (p.children) {
              p.children = [...p.children, c] ;
            } else {
              p.children = [c];
            }
          }
        });
      });
    };
    dataToTree(parents, children);
    return parents;
};
// setMapValue(dataSource,"0|0","参数名" "数据");
export const setMapValue = (map, levelIndex, fileld, value)=> {
  let levelIndexArr = levelIndex.split("|");
  if (levelIndexArr.length > 0) {
    let mapTemp;
    //遍历到最后一个层级
    for (let i = 0; i < levelIndexArr.length; i++) {
      if (i == 0) {
        mapTemp = map[parseInt(levelIndexArr[i])];
      } else {
        mapTemp = mapTemp.children[parseInt(levelIndexArr[i])];
      }
    }
    mapTemp[fileld] = value;
  }
}
/**
 * 
 * @param {*} val 
 * @returns 级联数组中去除children为空的层级
 */
export const getChildren = (val)=> {
  if(val.length >0){
      val.forEach((item, index, arr) => {
      if(item.children){
          if (item.children.length == 0) {
              delete item.children;
          }else {
              getChildren(item.children)
          }
      }
  })
  }
  return val
}

/**
 * 
 * @param {\} label 
 * @param {*} value 
 * @returns 获取label名称
 */
export const getLabel = (list,value)=>{
  let obj = {}
  obj = list.find(item=>{
      return item.value == value
  })
  return obj?obj.label:null
}

/**
 * @returns 树形过滤
 */
export const filterTree = (val, tree, newArr = []) => {
  if (!(tree.length && val)) {
    return tree;
  }
  for (let item of tree) {
    if (item.label.indexOf(val) > -1) {
      newArr.push(item);
      continue;
    }
    if (item.children && item.children.length) {
      let subArr = filterTree(val, item.children);
      if (subArr && subArr.length) {
        let node = { ...item, children: subArr };
        newArr.push(node);
      }
    }
  }
  return newArr;
};

/**
 * 过滤id不存在的数组
 * @param { Array } data 查找数组
 * @param { Array } ids id数组
 * @param { String } idKey id的键
 * @param {*} value 
 * @returns { Array } id数组
 */

export const isIdsArr = (data,ids,idKey = 'value') =>{
  let arr = []
  if(!ids )return arr
  ids.forEach(v => {
    const item = data.some(j => j[idKey] == v)
    if(item) arr.push(v)
  })
  return arr
}